/*
 * MIT License
 *
 * Copyright (c) 2023 芊芊墨客
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 */

package com.zhangmeng.tools.controller;

import cn.hutool.core.io.resource.ClassPathResource;
import cn.hutool.core.io.resource.Resource;
import com.zhangmeng.tools.utils.AlertUtils;
import com.zhangmeng.tools.utils.ImagePath;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.stage.DirectoryChooser;
import javafx.stage.Stage;
import lombok.extern.slf4j.Slf4j;

import java.io.*;
import java.util.Properties;

/**
 * @author : 芊芊墨客
 * @version : 1.0
 * @date : 2023-02-18 11:46
 */
@Slf4j
public class VideoSetController {

    @FXML
    private Button file_choose_button;

    @FXML
    private TextField text_file;

    private File file;

    @FXML
    public void initialize(){
        log.info("music set ....");
        file_choose_button.setText(null);
        ImageView iv = new ImageView(new Image(ImagePath.path(ImagePath.ImagePathType.IMAGE_FILE)));
        iv.setPreserveRatio(true);
        iv.setFitWidth(18);
        file_choose_button.setGraphic(iv);
    }

    @FXML
    public void choose_file(){
        Stage stage = (Stage) file_choose_button.getScene().getWindow();
        DirectoryChooser dc = new DirectoryChooser();
        dc.setTitle("文件夹选择器");
        file = dc.showDialog(stage);
        if (file != null){
            String path = file.getAbsolutePath();
            text_file.setText(path);
        }else {
            AlertUtils.alert_warning("请选择文件夹!");
        }
    }

    @FXML
    public void commit() throws FileNotFoundException {
        String path = text_file.getText();
        if (path.length() == 0 ){
            AlertUtils.alert_warning("请选择文件夹");
            return;
        }
        setPath(path);
        //关闭
        Stage stage = (Stage) text_file.getScene().getWindow();
        stage.close();

        if (file != null){
            VideoController.addDir(file);
        }

    }


    public void setPath(String path){
        Resource resource = new ClassPathResource("music.properties");
        InputStream inputStream = resource.getStream();
        FileOutputStream fileOutputStream = null;
        Properties properties = new Properties();
        try {
            properties.load(inputStream);
            String value = properties.getProperty("base.video.path");
            log.info("path:{}",value);
            properties.setProperty("base.video.path",path);
            log.info("properties:{}",properties);
            String out_path = Thread.currentThread().getContextClassLoader().getResource("").getPath() + "video.properties";
            fileOutputStream = new FileOutputStream(out_path);
            properties.store(fileOutputStream,"保存");
        } catch (IOException e) {
            log.info("选择路径出错!");
        }finally {
            try {
                inputStream.close();
                if (fileOutputStream != null){
                    fileOutputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
